package com.school.controller;

import com.alibaba.fastjson.JSONObject;
import com.school.entity.QueryRecord;
import com.school.service.QueryRecordService;
import com.school.untils.ResultEntity;
import com.school.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@Api(tags = "写实质疑管理")
@RestController
@RequestMapping("/queryRecord")
public class QueryRecordController {
    @Autowired
    QueryRecordService queryRecordService;

    // 增加
    @ApiOperation("写实质疑信息增加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultEntity addQueryRecord(HttpServletRequest servletRequest) throws IOException {
        List<MultipartFile> fileList = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            fileList = multipartRequest.getFiles("file");
        }
        JSONObject userJsonObject = (JSONObject) JSONObject.parse(servletRequest.getParameter("queryRecord"));
        QueryRecord queryRecord = JSONObject.toJavaObject(userJsonObject, QueryRecord.class);
        return  ResultUtils.build(queryRecordService.addQueryRecord(fileList,queryRecord));
    }

    // 根据写实记录id查看详情
    @ApiOperation("写实质疑信息查看详情")
    @GetMapping("/getDetailsByRecordId/{id}")
    public ResultEntity<Object> getDetailsByRecordId(@PathVariable("id") String id){
        return ResultUtils.build(queryRecordService.getDetailsByRecordId(id));
    }

    // 处理状态变更
    @ApiOperation("写实质疑信息处理状态变更")
    @RequestMapping(value = "/updateAudit",method = RequestMethod.POST)
    public void updateAudit(@RequestBody QueryRecord queryRecord){
        queryRecordService.updateAudit(queryRecord);
    }

    // 更新
    @ApiOperation("写实质疑信息更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public ResultEntity updateQueryRecord(HttpServletRequest servletRequest){
        JSONObject userJsonObject = (JSONObject) JSONObject.parse(servletRequest.getParameter("queryRecord"));
        QueryRecord queryRecord = JSONObject.toJavaObject(userJsonObject, QueryRecord.class);
        Integer audit = Integer.valueOf(servletRequest.getParameter("audit"));
        return ResultUtils.build(queryRecordService.updateQueryRecord(queryRecord,audit));
    }
}
